// mircoApp/cloud_shop/brand_home/brand_home.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    performanceActive: null,
    redColor: "#F15353",
    defaultColor: "#AAAAB3",
    // 品牌详情弹窗
    bandDetailShow: false,
    // 活动弹窗
    activeShow: false,
    // 筛选弹窗
    screen_show: false,

    infoData: [],
    nowTime: parseInt(new Date().getTime() / 1000),
    acitvePopupInfo: {
      info: {},
      key: "",
    },
    Source: [],
    first_category_id: "",
    second_category_id: "",
    third_category_id: "",
    // 二级显示数组
    second_category_arr: [],
    // 三级显示数组
    third_category_arr: [],
    collect_id: "",
    max_price: "",
    min_price: "",
    goodList: [],
    page: 1,
    total_page: 0,
    isLoadMore: true,
    // is_shop_owner是否是店主
    is_shop_owner: false,
    show_amount: true,
    order_column: "",
    order_sort: "",
    showSplice: false,
    showSplice_two: false,
    showSplice_thr: false,
    // 销售佣金
    self_reward_name: "省/赚",
    goodsInfo: [],
    popupSpecs: false,
    brand_follow_status: 0,
    poster_status: 0,
    hostUrl:app.data.host,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.hideShareMenu()
    if (options.id) {
      this.setData({
        brand_id: options.id,
      });
    }
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split('&');
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split('=');
          console.log(chil_arr[0])
          if (chil_arr[0] == 'bid') {
            this.data.brand_id = chil_arr[1];
          }
          console.log(this.data.brand_id)
          if (chil_arr[0] == 'mid') {
            app._setMid(chil_arr[1]);
          }
        }
      }
    }
    this.getData();
    let basic_info =
      wx.getStorageSync("yz_basic_info").plugin_setting?.cloud_shop;
    let language = wx.getStorageSync("langIndex");
    if (basic_info) {
      this.setData({
        is_shop_owner: basic_info.is_shop_owner,
        brand_follow_status: basic_info.brand_follow_status,
        self_reward_name: basic_info.self_reward_name,
        poster_status: basic_info.poster_status,
        language: language.en,
      });
    }
  },
  showSplice_tap() {
    this.setData({
      showSplice: !this.data.showSplice,
    });
  },
  tolink(e) {
    let {
      url,
      params
    } = e.currentTarget.dataset;
    if (url == 'diy') {
      let {
        item
      } = e.currentTarget.dataset;
      if (!item.mini_url) {
        app.tips('未设置专题链接')
        return
      }
      wx.navigateTo({
        url: item.mini_url,
      })
      return
    }
    wx.navigateTo({
      url: `${url}${params ? params : ""}`,
    });
  },
  onChange(e) {
    this.setData({
      timeData: e.detail,
    });
  },
  activeShowTap(e) {
    let {
      key,
      obj
    } = e.currentTarget.dataset;
    this.setData({
      "acitvePopupInfo.info": obj,
      "acitvePopupInfo.key": key,
      activeShow: true,
    });
  },
  activeShowClose(e) {
    this.setData({
      "acitvePopupInfo.info": {},
      "acitvePopupInfo.key": "",
      activeShow: false,
    });
  },
  async closeSpecsPopup(e) {
    let {
      cartid,
      clicktype,
      goodsCount,
      goodsId,
      optionsId
    } = e.detail;
    console.log(cartid, clicktype, goodsCount, goodsId, optionsId, e)
    if (clicktype == 0) {
      this.setData({
        popupSpecs: false,
        goodsInfo: []
      })
      return
    }
    let json = {
      total: goodsCount,
      option_id: optionsId,
      goods_id: goodsId
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("member.member-cart.store"),
      data: json,
    });
    if (data.result) {
      app.tips(data.msg);
      this.setData({
        popupSpecs: false,
        goodsInfo: []
      })
    } else {
      app.tips(data.msg);
    }
  },
  toCar(e) {
    let {
      item
    } = e.currentTarget.dataset;
    console.log(item)
    this.setData({
      popupSpecs: true,
      goodsInfo: item,
    });
  },
  show_amount_tap() {
    this.setData({
      show_amount: !this.data.show_amount,
    });
  },
  screen_show_open() {
    if (this.data.first_category_id) {
      this.getNextCate(2);
    }
    this.setData({
      screen_show: true,
    });
  },
  tapSortBtn(e) {
    let {
      key
    } = e.currentTarget.dataset;
    if (!this.data.order_column || this.data.order_column != key) {
      this.data.order_sort = "";
    }
    if (!this.data.order_sort || this.data.order_sort == "DESC") {
      this.data.order_sort = "ASC";
    } else {
      this.data.order_sort = "DESC";
    }
    this.setData({
      order_sort: this.data.order_sort,
      order_column: key,
    });
    this.init();
    this.getNextGood();
  },
  toShare() {
    let params = `/mircoApp/cloud_shop/cloud_shop_share/cloud_shop_share?brand_id=${this.data.brand_id}`;
    if (this.data.performanceActive != null) {
      let brand_collect_id =
        this.data.infoData.brand_collect_list[this.data.performanceActive].id;
      params += `&brand_collect_id=${brand_collect_id}`;
    }
    wx.navigateTo({
      url: `${params}`,
    });
  },
  tap_cate_active(e) {
    let {
      id
    } = e.currentTarget.dataset;
    this.setData({
      first_category_id: id,
      second_category_id: "",
      second_category_arr: [],
      third_category_arr: [],
      third_category_id: "",
    });
    this.init();
    this.getNextGood();
  },
  async getData() {
    let json = {
      brand_id: this.data.brand_id,
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.brand.index"),
      data: json,
    });
    if (data.result) {
      if (data.data.first_category_list.length > 0) {
        if (this.data.performanceActive == null) {
          this.setData({
            first_category_id: 0,
          });
        } else {
          this.setData({
            first_category_id: data.first_category_list[0].id,
          });
        }
      }
      this.setData({
        infoData: data.data,
      });
      this.getSource();
      this.init();
      this.getNextGood();
    } else {
      app.tips(data.msg);
    }
  },
  async getSource() {
    let json = {
      brand_id: this.data.brand_id,
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.material-center.api.centerlist.index"),
      data: json,
    });
    if (data.result) {
      this.setData({
        Source: data.data.list.data,
      });
    } else {
      app.tips(data.msg);
    }
  },
  async getNextGood() {
    let json = {
      brand_id: this.data.brand_id,
      first_category_id: this.data.first_category_id,
      second_category_id: this.data.second_category_id,
      third_category_id: this.data.third_category_id,
      max_price: this.data.max_price,
      min_pmin_price: this.data.min_price,
      order_column: this.data.order_column,
      order_sort: this.data.order_sort,
    };
    if (this.data.performanceActive != null) {
      json.collect_id =
        this.data.infoData.brand_collect_list[this.data.performanceActive].id;
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.brand.goodsList"),
      data: json,
    });
    if (data.result) {
      this.setData({
        goodList: data.data.list.data,
        total_page: data.data.list.last_page,
        isloadMore: true,
      });
    } else {
      app.tips(data.msg);
    }
  },
  async getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page += 1;
      let json = {
        page: this.data.page,
        brand_id: this.data.brand_id,
        first_category_id: this.data.first_category_id,
        second_category_id: this.data.second_category_id,
        third_category_id: this.data.third_category_id,
        max_price: this.data.max_price,
        min_pmin_price: this.data.min_price,
        order_column: this.data.order_column,
        order_sort: this.data.order_sort,
      };
      if (this.data.performanceActive != null) {
        json.collect_id =
          this.data.infoData.brand_collect_list[this.data.performanceActive].id;
      }
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.cloud-shop.frontend.brand.goodsList"),
        data: json,
      });
      this.data.isloadMore = true;
      if (data.result) {
        this.setData({
          goodList: this.data.goodList.concat(data.data.list.data),
        });
      } else {
        this.data.isloadMore = false;
        this.data.page = this.data.page - 1;
        app.tips(data.msg);
      }
    }
  },
  async getNextCate(key) {
    let json = {
      first_category_id: this.data.first_category_id,
    };
    if (key == 3 && this.data.infoData.cat_level == '3') {
      // 获取三级分类
      json.second_category_id = this.data.second_category_id;
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.brand.categoryList"),
      data: json,
    });
    if (data.result) {
      if (key == 2) {
        this.setData({
          second_category_arr: data.data.second_category_list,
        });
      } else {
        this.setData({
          third_category_arr: data.data.third_category_list,
        });
      }
    } else {
      app.tips(data.msg);
    }
  },
  async tapFollowM() {
    let json = {
      brand_id: this.data.brand_id,
      status: this.data.infoData.is_follow ? 0 : 1,
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.brand.follow"),
      data: json,
    });
    if (data.result) {
      this.setData({
        "infoData.is_follow": this.data.infoData.is_follow ? 0 : 1,
      });
      if (this.data.infoData.is_follow) {
        this.setData({
          "infoData.follow_count": this.data.infoData.follow_count + 1,
        });
      } else {
        this.setData({
          "infoData.follow_count": this.data.infoData.follow_count - 1,
        });
      }
      app.tips(data.msg);
    } else {
      app.tips(data.msg);
    }
  },
  tabChange(e) {
    this.setData({
      active: e.detail.index,
    });
  },
  bandDetailClose(e) {
    this.setData({
      bandDetailShow: false,
    });
  },
  bandDetailShow_open(e) {
    this.setData({
      bandDetailShow: true,
    });
  },
  toBrandSourceMaterial(e) {
    console.log(e);
    let {
      item
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: "/mircoApp/cloud_shop/brand_source_detail/brand_source_detail?id=" + item.id,
    });
  },
  activeShow_open(e) {
    this.setData({
      activeShow: true,
    });
  },
  activeShow_close() {
    this.setData({
      activeShow: false,
    });
  },
  screen_showClose() {
    this.setData({
      screen_show: false,
      showSplice: false,
      showSplice_two: false,
      showSplice_thr: false,
    });
  },
  showSplice_two_tap() {
    this.setData({
      showSplice_two: !this.data.showSplice_two,
    });
  },
  showSplice_thr_tap() {
    this.setData({
      showSplice_thr: !this.data.showSplice_thr,
    });
  },
  tap_performanceActive(e) {
    let {
      index
    } = e.currentTarget.dataset;
    this.setData({
      performanceActive: index == "null" ? null : index,
    });
    this.init();
    this.getNextGood();
  },
  async tapCateChild(e) {
    let {
      id,
      key
    } = e.currentTarget.dataset;
    if (key == 1) {
      this.setData({
        first_category_id: id,
        second_category_id: "",
        second_category_arr: [],
        third_category_id: "",
        third_category_arr: [],
      });
      this.getNextCate(2);
    }
    if (key == 2) {
      this.setData({
        second_category_id: id,
        third_category_id: "",
        third_category_arr: [],
      });
      this.getNextCate(3);
    }
    if (key == 3) {
      this.setData({
        third_category_id: id,
      });
    }
    console.log(key, "wwwwwwwdsadasda", this.data.second_category_id);
  },
  init() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
    });
  },
  tapConfirm() {
    this.init();
    this.getNextGood();
    this.setData({
      screen_show: false,
    });
  },
  resetTap() {
    this.setData({
      min_price: "",
      max_price: "",
      second_category_arr: [],
      second_category_id: "",
      third_category_arr: [],
      third_category_id: "",
      first_category_id: "",
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});